package form;

import com.example.demo.pdfbox.PdfGenerator;
import com.example.demo.pdfbox.common.PdfBorder;
import com.example.demo.pdfbox.common.PdfMargin;
import com.example.demo.pdfbox.common.enums.BorderStyle;
import com.example.demo.pdfbox.common.enums.Rectangle;
import com.example.demo.pdfbox.tabel.PdfPage;
import com.example.demo.pdfbox.tabel.PdfParagraph;
import com.example.demo.pdfbox.tabel.PdfTable;
import com.example.demo.pdfbox.tabel.concrete.PdfLine;
import com.example.demo.pdfbox.tabel.concrete.PdfPicture;
import com.example.demo.pdfbox.tabel.concrete.PdfText;
import com.example.demo.pdfbox.tabel.form.PdfColumn;
import com.example.demo.pdfbox.tabel.form.PdfForm;
import com.example.demo.pdfbox.tabel.form.PdfRow;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

import java.awt.*;
import java.io.IOException;
import java.util.Objects;

public class FormTest1 {
 public static void main(String[] args) throws IOException {
  // pdf表
  PdfTable pdfTable = new PdfTable();
  // 添加页，默认A4，边距正常
  PdfPage pdfPage = new PdfPage();
  pdfTable.addPage(pdfPage);
  // 页边距
  PdfMargin pageMargin = pdfPage.getCurrentPageMargin();
  // 页面参数
  PDRectangle rectangle = pdfPage.getCurrentPageRectangle();
  // 页面可用宽度平分一百份
  float unitWidth = (rectangle.getWidth() - pageMargin.getMarginLeft() - pageMargin.getMarginRight()) / 100;


  // 四列
  PdfForm pdfForm = new PdfForm(4, new float[]{unitWidth * 20, unitWidth * 20, unitWidth * 20, unitWidth * 20});
  // 偏移
  pdfForm.setX(15);
  pdfForm.setY(10);
  // 表格边框
  PdfBorder pdfBorder = new PdfBorder();
  pdfBorder.setBorderColor(Color.BLACK);
  pdfBorder.setBorderSize(1f);
  pdfBorder.setBorderStyle(BorderStyle.SOLID);
  pdfBorder.setBorderTangle(Rectangle.ALL_BORDER);
  pdfForm.setPdfBorder(pdfBorder);
  PdfRow pdfRow1 = new PdfRow();
  pdfRow1.setHeight(40);
  PdfParagraph pdfParagraph = new PdfParagraph();
  PdfText pdfText = new PdfText();
  pdfText.setName("column 11 ");
  pdfParagraph.addTextList(pdfText);
  pdfText = new PdfText();
  pdfText.setName("aaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbb ");
  pdfParagraph.addTextList(pdfText);
  PdfColumn pdfColumn1 = new PdfColumn();
  pdfColumn1.setPaddingTop(5);
  // pdfColumn1.setPaddingBottom(5);
  pdfColumn1.addElement(pdfParagraph);
  PdfColumn pdfColumn2 = new PdfColumn();

  PdfLine pdfLine = new PdfLine();
  pdfLine.setX(10);
  pdfLine.setY(10);
  pdfLine.setX1(0f).setY1(0f).setX4(20f).setY4(0f);
  pdfColumn2.addElement(pdfLine);

  pdfRow1.addColumn(pdfColumn1);
  pdfRow1.addColumn(pdfColumn2);

  PdfColumn pdfColumn3 = new PdfColumn();
  PdfPicture picture = new PdfPicture();
  picture.setX(5);
  picture.setY(25);
  byte[] imageByte = IOUtils.toByteArray(Objects.requireNonNull(PdfGenerator.class.getClassLoader().getResourceAsStream("logo.png")));
  picture.setHeight(20).setWidth(20).setImage(imageByte);
  pdfColumn3.addElement(picture);
  pdfRow1.addColumn(pdfColumn3);

  PdfColumn pdfColumn4 = new PdfColumn();
  PdfText pdfText1 = new PdfText();
  pdfText1.setName("column14");
  pdfColumn4.addElement(pdfText1);
  pdfRow1.addColumn(pdfColumn4);

  pdfForm.addRowList(pdfRow1);

  pdfPage.addElement(pdfForm);
  PdfGenerator pdfGenerator = new PdfGenerator();
  pdfGenerator.generatePdf(pdfTable,"D:\\mypdf.pdf");
 }
}
